class Solution
{
public:
    int deleteGreatestValue(vector<vector<int>> &grid)
    {
        int result = 0;
        int m = grid.size();
        int n = grid[0].size();
        for (int i = 0; i < n; ++i)
        {
            int maxToDel = 0;
            for (auto &row : grid)
            {
                auto rowMaxPos = max_element(row.begin(), row.end());
                maxToDel = max(maxToDel, *rowMaxPos);
                *rowMaxPos = -1;
            }
            result += maxToDel;
        }
        return result;
    }
};